<?php
/**
 * Flea Site
 *
 * @copyright  Copyright (c) 2008 Movoin Studio (http://movoin.com)
 * @license    Apache License 2.0
 * @version    $Id: Picturesloop.php 138 2009-05-25 06:42:38Z movoin $
 */

function _ctlPicturesloop($name, $args=null)
{
    $opts = array('classid', 'limit', 'site', 'width', 'height', 'class', 'controller', 'action');
    $data = FLEA_WebControls::extractAttribs($args, $opts);
    FLEA_WebControls::mergeAttribs($args);

    if(!$data['limit'])
        $data['limit'] = 10;
    if(!$data['site'])
        $data['site'] = 0;
    if(!$data['width'])
        $data['width'] = '100%';
    if(!$data['height'])
        $data['height'] = '100%';

    //缓存过期时间
    $deploy = defined('DEPLOY_MODE') && DEPLOY_MODE;
    if ( $deploy ) {
        $lifetime = 86400; //一天
    } else {
        $lifetime = 0;
    }
    //缓存名称
    $cache_id = 'picturesloop.' . $name . '.site' . $data['site'];
    if($data['classid'])
        $cache_id .= '.class' . $data['classid'];
    //从缓存中读取数据
    $content = FLEA::getCache($cache_id, $lifetime, true, true);
    if(!is_array($content)) {
        //载入产品模型
        $modelProducts =& FLEA::getSingleton( 'Model_Products' );
        $content = $modelProducts->getContents($name, null, (int)$data['limit'], 'id,title', 'attachments');
        FLEA::writeCache( $cache_id, $content, true );
    }
    $output = '';

    if(!function_exists(getImg)) {
        FLEA::loadFile( 'Helper_Common' );
    }

    // Loop Style
    $style = 'overflow:hidden; width:' . $data['width'] . '; height:' . $data['height'] . '; margin:0 auto;';

    // Loop Code
    $output .= '<div id="HI' . $name . '" style="' . $style .'">';
    $output .= '<table align="left" cellpadding="0" cellspace="0" border="0">';
    $output .= '<tr>';
    $output .= '<td id="HI' . $name . '1" class="' . $data['class'] . '">';
    // Loop Begin
    foreach( $content as $row ) {
        $output .= '<a href="' . url($data['controller'], $data['action'], array('pro'=>$row['id'])) . '">';
        $output .= getImg($row['attachments'], 'thumb');
        $output .= '</a>';
    }
    // Loop End
    $output .= '</td>';
    $output .= '<td id="HI' . $name . '2"' . $data['class'] . '></td>';
    $output .= '</tr>';
    $output .= '</table>';
    $output .= '</div>';
    $output .= "\n";
    $output .= '<script type="text/javascript">';
    $output .= "\n";
    $output .= 'var HI' . $name . ' = document.getElementById(\'HI' . $name . '\');';
    $output .= "\n";
    $output .= 'var HI' . $name . '1 = document.getElementById(\'HI' . $name . '1\');';
    $output .= "\n";
    $output .= 'var HI' . $name . '2 = document.getElementById(\'HI' . $name . '2\');';
    $output .= "\n";
    $output .= 'var speed=30;';
    $output .= "\n";
    $output .= 'HI' . $name . '2.innerHTML=HI' . $name . '1.innerHTML;';
    $output .= "\n";
    $output .= 'function Marquee(){';
    $output .= "\n";
    $output .= 'if(HI' . $name . '2.offsetWidth-HI' . $name . '.scrollLeft<=0){';
    $output .= "\n";
    $output .= "\n";
    $output .= 'HI' . $name . '.scrollLeft-=HI' . $name . '1.offsetWidth;';
    $output .= "\n";
    $output .= '}else{';
    $output .= "\n";
    $output .= 'HI' . $name . '.scrollLeft++;';
    $output .= "\n";
    $output .= '}';
    $output .= "\n";
    $output .= '}';
    $output .= "\n";
    $output .= 'var MyMar=setInterval(Marquee,speed);';
    $output .= "\n";
    $output .= 'HI' . $name . '.onmouseover=function() { clearInterval(MyMar); }';
    $output .= "\n";
    $output .= 'HI' . $name . '.onmouseout=function() { MyMar=setInterval(Marquee,speed); }';
    $output .= "\n";
    $output .= '</script>';
    $output .= "\n";

    return $output;
}
